﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchEdu.Framework.DataModel;

namespace SchEduApp
{
    public partial class GeneratorForm : Form
    {
        public GeneratorForm()
        {
            InitializeComponent();
        }

        private void generateButton_Click(object sender, EventArgs e)
        {
            int numberOfOrders;
            if (Int32.TryParse(this.numOrdersTextBox.Text, out numberOfOrders) == false)
            {
                numberOfOrders = 0;
            }
            int minOperations;
            if (Int32.TryParse(this.minOperationsTextBox.Text, out minOperations) == false)
            {
                minOperations = 0;
            }

            int maxOperations;
            if (Int32.TryParse(this.maxOperationsTextBox.Text, out maxOperations) == false)
            {
                maxOperations = 0;
            }

            if (numberOfOrders <= 0)
            {
                MessageBox.Show("Invalid number of orders");
                return;
            }
            if (minOperations <= 0)
            {
                MessageBox.Show("Invalid minimum number of operations");
                return;
            }
            if (maxOperations <= 0)
            {
                MessageBox.Show("Invalid maximum number of operations");
                return;
            }

            if (maxOperations < minOperations)
            {
                MessageBox.Show("Invalid maximum/minumum number of operations");
                return;
            }

            Generate(numberOfOrders, minOperations, maxOperations);

            Close();
            
        }

        void Generate(int numberOfOrders, int minOperations, int maxOperations)
        {
            Random numOperationRandom = new Random(System.DateTime.Now.Second);
            Random workCenterIndexRandom = new Random(numOperationRandom.Next());
            Random processingTimeRandom = new Random(workCenterIndexRandom.Next());
            for (int i = SchEduAppContext.SchEduData.WorkCenter.Count; i < maxOperations; i++)
            {
                SchEduDataSet.WorkCenterRow workCenter = SchEduAppContext.SchEduData.WorkCenter.NewWorkCenterRow();
                SchEduAppContext.SchEduData.WorkCenter.AddWorkCenterRow(workCenter);
                workCenter.Name = string.Format("WC{0}", SchEduAppContext.SchEduData.WorkCenter.Count);

                SchEduDataSet.MachineRow machine = SchEduAppContext.SchEduData.Machine.NewMachineRow();
                machine.WorkCenterID = workCenter.ID;
                SchEduAppContext.SchEduData.Machine.AddMachineRow(machine);
                machine.Name = string.Format("MC{0}", machine.ID);

            }

            for (int i = 0; i < numberOfOrders; i++)
            {
                SchEduDataSet.OrderRow order = SchEduAppContext.SchEduData.Order.NewOrderRow();
                SchEduAppContext.SchEduData.Order.AddOrderRow(order);
                order.OrderNo = string.Format("WO{0}", SchEduAppContext.SchEduData.Order.Count);
                order.DueDate = 0;
                int numOfOperations = numOperationRandom.Next(minOperations,maxOperations);
                for (int j = 0; j < numOfOperations; j++)
                {
                    SchEduDataSet.OperationRow operation = SchEduAppContext.SchEduData.Operation.NewOperationRow();
                    operation.OrderID = order.ID;
                    int workCenterIndex = workCenterIndexRandom.Next(maxOperations);
                    operation.WorkCenterID = SchEduAppContext.SchEduData.WorkCenter[workCenterIndex].ID;
                    operation.ProcessingTime = processingTimeRandom.Next(1,10);
                    operation.SequenceNo = j+1;
                    SchEduAppContext.SchEduData.Operation.AddOperationRow(operation);
                    operation.OperationNo = string.Format("OP{0}", order.GetOperationRows().Count());
                }
            }

        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
